import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'
import store from 'store'

const base = url.base,
  getDeveloperManageList = request.getDeveloperManageList,
  findBindCard = '/admin/bindCard/findBindCard.json'

let table,
  storeData;

// 初始化表格
table = $('#table').bootstrapTable($.extend({}, tableConfig, {
  url: base + findBindCard,
  queryParams: function (params) {
    return $.extend({
      currentPage: params.pageNumber,
      pageSize: params.pageSize,
    }, $('#searchForm').serializeObject())
  },
  columns: [
    {
      field: 'realName',
      title: '姓名',
      valign: 'middle'
    }, {
      field: 'mobile',
      title: '手机号',
      valign: 'middle'
    }, {
      field: '',
      title: '渠道银行',
      valign: 'middle',
      formatter: function (index, row) {
        return '民生银行'
      }
    }, {
      field: 'rawAddTime',
      title: '申请时间',
      valign: 'middle',
      formatter: function (index, row) {
        let date = new Date(row.rawAddTime)
        return date.toLocaleDateString() + date.toLocaleTimeString()
      }
    }, {
      field: 'status',
      title: '状态',
      valign: 'middle',
      formatter: function (index, row) {
        let status = row.status
        switch (status) {
          case '0':
            status = '待审核'
            break
          case '1':
            status = '审核中'
            break
          case '2':
            status = '审核失败'
            break
          case '3':
            status = '待确认'
            break
          case '4':
            status = '变更成功'
            break
        }
        return status
      }
    }, {
      field: 'feedbackTime',
      title: '反馈时间',
      valign: 'middle',
      formatter: function (index, row) {
        let date = new Date(row.feedbackTime)
        return date.toLocaleDateString() + date.toLocaleTimeString()
      }
    }, {
      field: 'message',
      title: '反馈原因',
      valign: 'middle'
    }, {
      field: '',
      valign: 'middle',
      align: 'center',
      title: '操作'
    }]
}));

// 点击列表的时候传递数据到全局变量
table.on('click-row.bs.table', (e, data, el) => {
  storeData = data
});

// 初始化时间选择器
$('#startDate').datetimepicker({
  format: "yyyy-mm-dd",
  startView: 3,
  minView: 2,
  autoclose: true
});

$('#endDate').datetimepicker({
  format: "yyyy-mm-dd",
  startView: 3,
  minView: 2,
  autoclose: true
});

// 搜索
$('#search-btn').on('click', (e) => {
  e.preventDefault();

  table.bootstrapTable('refresh', {
    query: $.extend({
      currentPage: 1,
    }, $('#form').serializeObject(), $('#form-2').serializeObject())
  })
});

// 修改
$(document).on('click', '#table .btn-modify', (e) => {
  e.preventDefault()

  console.log('修改')

  store.set('storeData', storeData)
  window.location.href = `./modify.html?id=${ storeData.id }`
});

// 删除
$(document).on('click', '#table .btn-delete', (e) => {
  e.preventDefault()

  let id = $(e.target).attr('data-id'),
    data = {id: id};

  bootbox.confirm({
    title: '提示',
    message: `<div>
                <h3 style="margin:.75em 0">是否删除该清算银行信息？</h3>
              </div>`,
    buttons: {
      cancel: {
        label: '取消'
      },
      confirm: {
        label: '确认'
      }
    },
    callback: (result) => {

      if (!result) {
        return
      }

      let loading_modal = layer.msg('提交中...', {
        icon: 16,
        time: 0
      })

      $.ajax({
        headers: header,
        url: base + OnePCMonitorPublish,
        type: 'POST',
        data: data
      })
        .done(function (res) {
          console.log(res)

          if (res.code === 0) {

            layer.msg('删除成功', {
              icon: 6
            })

            table.bootstrapTable('refresh')

          } else {
            layer.msg(res.msg, {
              icon: 5
            })
          }

        })
        .fail(function (res) {
          console.log(res)

          layer.msg('删除失败', {
            icon: 5
          })

        })
        .always(function (res) {

          console.log("complete")

          layer.close(loading_modal)
        })
    }
  })
});
